﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace PlayerStat
{
    public class Translate
    {
        //string should be as "week date month year"
        public static DateTime TransFrDateTime(string str)
        {
            try
            {
                string[] strs = str.Split(' ');
                if (strs.Length == 4)
                {
                    int date = int.Parse(strs[1]);
                    int month = GetMonthFR(strs[2]);
                    int year = int.Parse(strs[3]);
                    return DateTime.Parse(string.Format("{0}-{1}-{2}",year,month,date));
                }
                else
                {
                    throw new Exception("Invilid string for date parsing.");
                }

                return DateTime.Parse(str);
            }
            catch(Exception ex)
            {
                return DateTime.Parse("9999/12/31");
            }
        }
        
        //string should be as "week date month year"
        public static DateTime TransDateTime(string str)
        {
            try
            {
                string[] strs = str.Split(' ');
                if (strs.Length == 4)
                {
                    int date = GetDay(strs[1]);
                    int month = GetMonth(strs[2]);
                    int year = int.Parse(strs[3]);
                    return DateTime.Parse(string.Format("{0}-{1}-{2}", year, month, date));
                }
                else
                {
                    throw new Exception("Invilid string for date parsing.");
                }
                return DateTime.Parse(str);
            }
            catch (Exception ex)
            {
                return DateTime.Parse("9999/12/31");
            }
        }
        
        public static int GetMonthFR(string str)
        {
            int month = 0;
            switch (str.Trim().ToLower())
            {
                case "janvier":
                    month = 1;
                    break;
                case "février":
                    month = 2;
                    break;
                case "mars":
                    month = 3;
                    break;
                case "avril":
                    month = 4;
                    break;
                case "mai":
                    month = 5;
                    break;
                case "juin":
                    month = 6;
                    break;
                case "juillet":
                    month = 7;
                    break;
                case "août":
                    month = 8;
                    break;
                case "septembre":
                    month = 9;
                    break;
                case "octobre":
                    month = 10;
                    break;
                case "novembre":
                    month = 11;
                    break;
                case "décembre":
                    month = 12;
                    break;
                default:
                    throw new Exception("Can not translate " + str + "to month");
            }
            return month;
        }
        
        public static int GetDay(string str)
        {
            try
            {
                string sday = str.Trim().Substring(0,str.Length-2);
                return int.Parse(sday);
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("Can not translate the str({0}) to day value.",str));
            }
        }
        
        public static int GetMonth(string str)
        {
            int month = 0;
            switch (str.Trim().ToLower())
            {
                case "january":
                    month = 1;
                    break;
                case "febury":
                    month = 2;
                    break;
                case "march":
                    month = 3;
                    break;
                case "april":
                    month = 4;
                    break;
                case "may":
                    month = 5;
                    break;
                case "june":
                    month = 6;
                    break;
                case "july":
                    month = 7;
                    break;
                case "august":
                    month = 8;
                    break;
                case "september":
                    month = 9;
                    break;
                case "october":
                    month = 10;
                    break;
                case "november":
                    month = 11;
                    break;
                case "december":
                    month = 12;
                    break;
                default:
                    throw new Exception("Can not translate " + str + "to month");
            }
            return month;
        }
    
		//string should be as "date,time" such as "28.07.15:30"
        public static DateTime TransGerDateTime(string str, int seasonStartYear)
        {
            try
            {
                string[] strs = str.Split('.');
                if (strs.Length == 3)
                {
                    int day = int.Parse(strs[0]);
                    int month = int.Parse(strs[1]);
                    int year = (month >= 7) ? seasonStartYear : seasonStartYear + 1;

                    string strDateTime = string.Format("{0}-{1}-{2} {3}", year, month, day, strs[2]);
                    return DateTime.Parse(strDateTime);
                }
                else
                {
                    throw new Exception("Invalid string for date parsing.");
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.Write(ex.Message);
                return DateTime.Parse("9999/12/31");
            }
        }

        //string strDate should be as "date" such as "fredag 18.03.2011"
        public static DateTime TransNorwayDateTime(string strDate, string strTime)
        {
            try
            {
                string[] strs = strDate.Split('.', ' ');
                if (strs.Length == 4)
                {
                    int day = int.Parse(strs[1]);
                    int month = int.Parse(strs[2]);
                    int year = int.Parse(strs[3]);

                    string strDateTime = string.Format("{0}-{1}-{2} {3}", year, month, day, strTime);
                    return DateTime.Parse(strDateTime);
                }
                else
                {
                    throw new Exception("Invalid string for date parsing.");
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.Write(ex.Message);
                return DateTime.Parse("9999/12/31");
            }
        }

        public static DateTime TransPorDateTime(int year, string date, string time)
        {
            string[] temp = date.Split('/');
            int day = int.Parse(temp[0]);
            int month = GetMonthPor(temp[1]);
            temp = time.Split('h');
            int hour = int.Parse(temp[0]);
            int minute = int.Parse(temp[1]);
            return DateTime.Parse(string.Format("{0}-{1}-{2} {3}:{4}",year,month,day,hour,minute)) ;
        }
        
        public static int GetMonthPor(string str)
        {
            int month = 0;
            switch (str.Trim().ToLower())
            {
                case "jan":
                    month = 1;
                    break;
                case "fev":
                    month = 2;
                    break;
                case "mar":
                    month = 3;
                    break;
                case "abr":
                    month = 4;
                    break;
                case "mai":
                    month = 5;
                    break;
                case "jun":
                    month = 6;
                    break;
                case "jul":
                    month = 7;
                    break;
                case "ago":
                    month = 8;
                    break;
                case "set":
                    month = 9;
                    break;
                case "out":
                    month = 10;
                    break;
                case "nov":
                    month = 11;
                    break;
                case "dez":
                    month = 12;
                    break;
                default:
                    throw new Exception("Can not translate " + str + "to month");
            }
            return month;
        }
		
        //string strDate should be as "date" such as "2011-04-02 16:00"
        public static DateTime TransSwedenDateTime(string strDateTime)
        {
            try
            {
                    return DateTime.Parse(strDateTime);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.Write(ex.Message);
                return DateTime.Parse("9999/12/31");
            }
        }

        //string strDate should be as "date" such as "16-07-2011 17:00"
        public static DateTime TransDenmarkDateTime(string strDateTime)
        {
            try
            {
                IFormatProvider culture = new CultureInfo("da-DK", true);

                return DateTime.Parse(strDateTime, culture);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.Write(ex.Message);
                return DateTime.Parse("9999/12/31");
            }
        }
	}
}
